from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from base import *

def _load_payment_backend():
	backend_path = getattr(settings, 'PAYMENT_BACKEND', 'accounts.payment.DummyPaymentBackend')
	i = backend_path.rfind('.')
	module, attr = backend_path[:i], backend_path[i+1:]
	try:
		mod = __import__(module, {}, {}, [attr])
	except ImportError, e:
		raise ImproperlyConfigured, 'Error importing payment backend %s: "%s"' % (module, e)
	except ValueError, e:
		raise ImproperlyConfigured, 'Error importing payment backend. Is PAYMENT_BACKEND a correctly defined path to a PaymentBackend class?'
	try:
		cls = getattr(mod, attr)
	except AttributeError:
		raise ImproperlyConfigured, 'Module "%s" does not define a "%s" payment backend' % (module, attr)
	return cls()

gateway = _load_payment_backend()